package com.sara.service.impl;


import com.sara.entity.Employee;
import com.sara.entity.EmployeeExample;
import com.sara.entity.EmployeeRole;
import com.sara.entity.EmployeeRoleExample;
import com.sara.mapper.EmployeeMapper;
import com.sara.mapper.EmployeeRoleMapper;
import com.sara.service.EmployeeService;
import com.sara.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;


    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;


    /**
     * 根据用户电话查找用户对象
     *
     * @param userTel
     * @return 用户对象
     */
    @Override
    public Employee findByEmployeeTel(String userTel) {
        EmployeeExample employeeExample = new EmployeeExample();
        employeeExample.createCriteria().andEmployeeTelEqualTo(userTel);
        List<Employee> employees = employeeMapper.selectByExample(employeeExample);
        Employee employee = null;
        if(employees != null && employees.size() > 0){
            employee = employees.get(0);
        }
        
        return employee;
    }

    /**
     * 通过参数去查找员工列表
     *
     * @param pageNum
     * @param params
     * @return
     */
    @Override
    public Page<Employee> findByParams(Integer pageNum, Map<String, Object> params) {
        int count = employeeMapper.count();
        System.out.println("总共的员工人数是：" + count);
        Page<Employee> page = new Page<>(Integer.valueOf(pageNum), count);

        List<Employee> employeeList = employeeMapper.selectByParams(params);
        page.setTList(employeeList);
        return page;
    }

    @Override
    public void save(Employee employee, Integer[] roleIds) {
        EmployeeRole employeeRole = new EmployeeRole();
        employee.setState(1);
        employeeMapper.insertSelective(employee);
        System.out.println("返回的employeeidshi" + employee.getId());
        System.out.println("roleids的长度是："+ roleIds.length);
        if (roleIds != null){

            for(Integer role : roleIds){
                employeeRole.setEmployeeId(employee.getId());
                employeeRole.setRoleId(role);

                employeeRoleMapper.insert(employeeRole);
                System.out.println("执行连表插入第...次" );
            }

        }
    }

    /**
     * 通过员工的id去查询员工的全部信息
     *
     * @param id
     * @return
     */
    @Override
    public Employee findEmployeeById(Integer id) {
       return employeeMapper.selectByPrimaryKey(id);
    }

    /**
     * 修改员工信息
     *
     * @param employee
     * @param roleIds
     */
    @Override
    public void editEmployee(Employee employee, Integer[] roleIds) {
        EmployeeRole employeeRole = new EmployeeRole();
        employeeMapper.updateByPrimaryKeySelective(employee);
        EmployeeRoleExample employeeRoleExample = new EmployeeRoleExample();
        employeeRoleExample.createCriteria().andEmployeeIdEqualTo(employee.getId());
        if(roleIds != null){

            employeeRoleMapper.deleteByExample(employeeRoleExample);
            for(Integer role : roleIds){
                employeeRole.setEmployeeId(employee.getId());
                employeeRole.setRoleId(role);

                employeeRoleMapper.insert(employeeRole);
            }
        }
    }

    @Override
    public void deleteEmployee(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
        EmployeeRoleExample employeeRoleExample = new EmployeeRoleExample();
        employeeRoleExample.createCriteria().andEmployeeIdEqualTo(id);
        employeeRoleMapper.deleteByExample(employeeRoleExample);
    }

    @Override
    public void editEmployeeState(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);

        Integer state = employee.getState();
        state = 1 - state;
        employee.setState(state);
        employeeMapper.updateByPrimaryKeySelective(employee);
        System.out.println("获得的员工的状态是" + employee.getState());



        /*if("1"== employee.getState()){
            state = "0";
            employee.setState(state);
            employeeMapper.updateByPrimaryKeySelective(employee);
        }
        if("0"== employee.getState()){
            state = "1";
            employee.setState(state);
            employeeMapper.updateByPrimaryKeySelective(employee);
        }*/

    }


}
